How To Combine Dynamic And Static Routes In React

您所在的位置:网站首页 react router exact How To Combine Dynamic And Static Routes In React

How To Combine Dynamic And Static Routes In React

#How To Combine Dynamic And Static Routes In React| 来源: 网络整理| 查看: 265

React is a highly popular js library which makes writing frontend a breeze, it makes javascript make more sense by laying out UIs in components which acts and behaves independently. React is one of the go to libraries for modern web development.

There are 3 suggested solutions in this post and each one is listed below with a detailed description on the basis of most helpful answers as shared by the users. I have tried to cover all the aspects as briefly as possible covering topics such as Javascript, Reactjs and a few others. I have categorized the possible solutions in sections for a clear and precise explanation. Please consider going through all the sections to better understand the solutions.

Contents 01 How to combine dynamic and static routes in React 02 Solution 1 03 Solution 2 04 Solution 3 05 Final Words Solution 1

You are missing exact:

Install Solution 2

Explaining my initial comment.

As stated, you are missing exact in your Route. The exact param comes into play when you have multiple paths that have similar names (your initial problem)

Install

What does exact do?

The exact param disables the partial matching for a route and makes sure that it only returns the route if the path is an EXACT match to the current url.

Where you should define routes?

App.js is a good starting point for main routes. If you have nested components like Parent > Child, and each chuld can be different, then define Parent in App.js and Inside Parent Component, define routes for your Child.

In general, it all depends on your app flow and Architecture

Solution 3

Here's the final solution you can try out in case no other solution was helpful to you. This one's applicable and useful in some cases and could possiblty be of some help. No worries if you're unsure about it but I'd recommend going through it.

You could try to remove the object in your path prop

Change this

for

It's the only thing I saw as a problem here, I've wrote your code just to test and it works

Check it here

Final Words

React has become the go to library for frontend development. It has a wide community support and a multitude of ready made components. These were few of the solutions reported helpful by the community. Hope it turns out helpful for you as well.

❝ When your computer starts falling apart, stop hitting it with a Hammer! Unknown ❞


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3